package com.ibilibili.download;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
import java.io.File;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VideoDownload {
    private String mUrl;    // 下载连接字符串
    private File saveFileName = null;   // 保存文件路径和名称

    // 构造函数
    public VideoDownload() {}
    public VideoDownload(String murl, String saveFileName) {
        this.setmUrl(murl);
        this.setSaveFileName(new File(saveFileName));
    }

    /**
     * desc: 解析html页面并获取下载连接
     * @param murl 下载视频所在的页面
     * @return 解析后
     */
    private String getUrl(String murl) {
        String downUrl = "";
        try {
            Document doc = Jsoup.connect(murl).get();
            Elements elements = doc.select("a[href]");
            Pattern p = Pattern.compile("视频下载.*");
            for (Element e:elements) {
                String text = e.text();
                Matcher match = p.matcher(text);
                if (match.find()) {
                    downUrl = e.attr("abs:href");
                    break;
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return downUrl;

    }

    public void down() {
        this.down(this.getmUrl(), this.getSaveFileName());
    }

    /**
     * desc: 下载视频
     * @param mUrl 下载视频所在的页面
     * @param saveFileName 为一个全名(路径+文件名)文件
     */
    public void down(String mUrl, File saveFileName) {
        String downUrl = this.getUrl(mUrl);
        System.out.println("downUrl: " + downUrl);
        if ("".equals(downUrl)) {
            System.out.println("downUrl is none, return");
            return;
        }
        System.out.println("down load begin ....");
        saveUrlAs(downUrl, saveFileName);
        System.out.println("down load end ....");
    }

    /**
     * desc: 将文件保存到本地(注意: 此方法只能用HTTP协议)
     * @param mUrl 文件下载地址
     * @param fileName 为一个全名(路径+文件名)文件
     */
    public static void saveUrlAs(String mUrl, File fileName){
        URL url;
        DataOutputStream out = null;        // 输出流
        DataInputStream in = null;          // 输入流
        try {
            url = new URL(mUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            in = new DataInputStream(connection.getInputStream());
            out = new DataOutputStream(new FileOutputStream(fileName));
            byte[] buffer = new byte[4096];
            int count = 0;
            // 每次读写4096字节, 直至完成
            while ((count = in.read(buffer)) > 0) {
                out.write(buffer, 0, count);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(out != null){
                    out.close();
                }
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public File getSaveFileName() {
        return saveFileName;
    }

    public void setSaveFileName(File saveFileName) {
        this.saveFileName = saveFileName;
    }

    public String getmUrl() {
        return mUrl;
    }

    public void setmUrl(String mUrl) {

        this.mUrl = mUrl;
    }
}
